<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-12
 * Time: 12:47
 */
namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Session;

class Orders extends Controller
{
    public function getIndex(){
        $user_id=Session::get('userinfo')['user_id'];
       // $res=Db::table('tp_orders')->where('user_id',$user_id)->where('status','!=5')->select();
       $res=Db::table('tp_orders')->alias('to')
           ->field('to.id,to.address_id,to.total,to.create_time,to.status,ta.address_name,ta.address_address,ta.address_detailed,ta.address_phone')
           ->join('tp_address ta','to.address_id=ta.id','left')
           ->where('user_id',$user_id)
           ->select();
        $i=0;
        foreach($res as $v) {
          $res[$i]['create_time'] = date('Y-m-d', $v['create_time']);
          $i++;
        }
        $this->assign('res',$res);
        return $this->fetch('orders/index');
    }


    public function getDetail()
    {
        $request=$this->request;
        $ress=$request->except('action');
        $res=Db::table('tp_orders')->alias('to')
            ->field('to.id,tod.id,tod.orders_id,tod.goods_id,tod.goods_num,tg.goods_name,tg.goods_pic,tg.goods_price')
            ->join('tp_orders_detail tod','to.id=tod.orders_id')
            ->join('tp_goods tg','tod.goods_id=tg.id')
            ->where('to.id',$ress['id'])
            ->select();
        $this->assign('res',$res);
        return $this->fetch('/orders/order_detail');
    }

    public function postaces()
    {
        $request=$this->request;
        $res=$request->except('action');
        $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'3']);
        if($result){
          $data=
            [
                'status'=>0,
                'msg'=>'收货',
                'id'=>$res['id'],
            ];
        }else{
          $data=
            [
                'status'=>1,
                'msg'=>'出现未知错误，请稍后重试',
                'id'=>$res['id'],
            ];
        }
        return json($data);
    }
}